package com.stedu.mall.orderservice.controller;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import com.stedu.mall.common.bean.Order;
import com.stedu.mall.common.bean.OrderVo;
import com.stedu.mall.common.bean.RespBean;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.OrderService;
import com.stedu.mall.common.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping
    public RespBean insert(@RequestBody OrderVo orderVo, @RequestHeader("token") String token) throws SteduException {
        //不需要获取jwt,直接解析
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        //获取用户id
        Integer userId = (Integer) map.get("id");
        orderVo.setUserId(userId);

        orderService.insert(orderVo);
        return RespBean.ok("添加成功", orderVo);
    }

    //用户自己用的
    @GetMapping("/{id}")
    public RespBean selectById(@PathVariable("id") String id, @RequestHeader("token") String token) throws SteduException {
        //不需要获取jwt,直接解析
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        //获取用户id
        Integer userId = (Integer) map.get("id");

        //查询订单信息
        Order order = orderService.selectById(id, userId);
        return RespBean.ok("根据id查询订单成功", order);
    }

    //管理员用的
    @GetMapping("/admin/{id}")
    public RespBean selectById1(@PathVariable("id") String id) throws SteduException {
        //查询订单信息
        Order order = orderService.selectById1(id);
        return RespBean.ok("根据id查询订单成功", order);
    }

    //用户查询自己的订单
    @GetMapping("/selectAll")
    public RespBean selectAll(@RequestHeader("token") String token, Order condition, Integer pageNum, Integer pageSize) {
        //不需要获取jwt,直接解析
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        //获取用户id
        Integer userId = (Integer) map.get("id");
        condition.setUserId(userId);

        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;
        PageInfo<Order> pageInfo = orderService.selectByCondition(condition, pageNum, pageSize);
        return RespBean.ok("查询成功", pageInfo);
    }

    //管理员查询所有的订单
    @GetMapping("/selectAll1")
    public RespBean selectAll1(Order condition, Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;
        PageInfo<Order> pageInfo = orderService.selectByCondition(condition, pageNum, pageSize);
        return RespBean.ok("查询成功", pageInfo);
    }

    @PostMapping("/pay")
    public RespBean pay(@RequestBody OrderVo orderVo, @RequestHeader("token") String token) throws SteduException {
        //不需要获取jwt,直接解析
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        //获取用户id
        Integer userId = (Integer) map.get("id");
        orderVo.setUserId(userId);

        orderService.pay(orderVo);
        return RespBean.ok("支付成功0.0", orderVo);
    }
    @PostMapping("/pay1")
    public RespBean pay1(@RequestBody OrderVo orderVo, @RequestHeader("token") String token) throws SteduException {
        //不需要获取jwt,直接解析
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        //获取用户id
        Integer userId = (Integer) map.get("id");
        orderVo.setUserId(userId);

        orderService.pay1(orderVo);
        return RespBean.ok("支付成功0.0", orderVo);
    }

    @PutMapping("/update")
    public RespBean update(@RequestBody Order order, @RequestHeader("token") String token) {
        //解析token
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        order.setUserId(userId);
        orderService.update(order);
        return RespBean.ok("修改成功");
    }

    @PutMapping("/update1")
    public RespBean update1(@RequestBody Order order) {
        orderService.update(order);
        return RespBean.ok("修改成功");
    }

    @PutMapping("/{id}")
    public RespBean updateStatus(@PathVariable("id") String id) throws SteduException {
        Order order = orderService.selectById1(id);
        order.setStatus(2);
        String express = IdUtil.getSnowflakeNextIdStr();
        order.setExpress(express);
        orderService.update(order);
        return RespBean.ok("修改成功");
    }

    @PutMapping("/updateGoodsMoney/{id}")
    public RespBean update2(@PathVariable("id") String id, @RequestHeader("token") String token) throws SteduException {
        //不需要获取jwt,直接解析
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        //获取用户id
        Integer userId = (Integer) map.get("id");

        Order order = orderService.selectById(id, userId);
        orderService.updateGoodsMoney(order);
        return RespBean.ok("正在联系商家为您退货退款,请等待!!");
    }
    @PutMapping("/updateGoodsMoney1/{id}")
    public RespBean update22(@PathVariable("id") String id) throws SteduException {
        orderService.updateGoodsMoney1(id);
        return RespBean.ok("正在为客户进行退货退款中!");
    }
    @PutMapping("/updateMoney/{id}")
    public RespBean update4(@PathVariable("id") String id, @RequestHeader("token") String token) throws SteduException {
        //不需要获取jwt,直接解析
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        //获取用户id
        Integer userId = (Integer) map.get("id");

        Order order = orderService.selectById(id, userId);
        orderService.updateMoney(order);
        return RespBean.ok("正在联系商家,请耐等待");
    }

    @PutMapping("/updateMoney1/{id}")
    public RespBean update44(@PathVariable("id") String id) throws SteduException {
        orderService.updateMoney1(id);
        return RespBean.ok("正在为客户进行退款中!");
    }
}
